﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using ProjectA.Controls;
using System.Threading;
using System.ComponentModel;

namespace ProjectA.Page_Login
{
    //public class cPage_Login : cPage
    public class cPage_Login : Panel
    {
        public cPage_Login(frmPrincipal frmPrincipal)
        {
            this.frmPrincipal = frmPrincipal;
            //ImageBackGroundPage = ((System.Drawing.Image)(Properties.Resources.bg_login));
            this.Location = new Point(0, 0);
            this.Size = new System.Drawing.Size(884, 662);
            this.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.bg_login));
            Initialize();
        }

        void worker_DoWork(object sender, DoWorkEventArgs e){}

        void worker_btLoginIngregar_Click(object sender, RunWorkerCompletedEventArgs e)
        {
            
            try
            {
                lLogErrInfo.Text = frmPrincipal.cPage_General.SubPage_Config.WindowUsuarios.cConfigUsers.LoginUser(tbLoginUsuario.Text, tbLoginPassword.Text, out frmPrincipal.cPage_General.SubPage_Config.WindowUsuarios.cUsuario);
                if (frmPrincipal.cPage_General.SubPage_Config.WindowUsuarios.cUsuario.UserName != null)
                {
                    if (frmPrincipal.cPage_General.SubPage_Config.WindowUsuarios.cUsuario.UserState)
                    {
                        this.Visible = false;
                        frmPrincipal.cPage_General.Visible = true;
                        frmPrincipal.Update();
                        frmPrincipal.cPage_General.cbCabeceraSeleccionarProyecto.SelectedIndex = -1;
                        lLogErrInfo.Text = "";
                        if (frmPrincipal.cPage_General.SubPage_Config.WindowUsuarios.cUsuario.Role == "Administrador")
                            frmPrincipal.cPage_General.btCabeceraConfig.Enabled = true;
                        else
                            frmPrincipal.cPage_General.btCabeceraConfig.Enabled = false;
                    }
                    else
                        lLogErrInfo.Text = "Cuenta Inactiva!";
                }
            }
            catch (Exception)
            {
            }
            frmPrincipal.cPage_General.cbCabeceraSeleccionarProyecto.SelectedIndex = -1;
            this.frmPrincipal.frmLoad.Close();
        }


        private void btLoginIngregar_Click(object sender, EventArgs e)
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_btLoginIngregar_Click);
            worker.RunWorkerAsync();

            this.frmPrincipal.frmLoad.ShowDialog();
        }

        private void Initialize()
        {
            btLoginIngregar = new cButton(new Point(381, 430), new Size(122, 33), new EventHandler(btLoginIngregar_Click), Properties.Resources.bt_ingresar_1, Properties.Resources.bt_ingresar_2, Properties.Resources.bt_ingresar_3, Properties.Resources.bt_ingresar_4);
            tbLoginUsuario = new cTextBox(new Point(308, 329), new Size(267, 19), new Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Color.FromArgb(((int)(((byte)(233)))), ((int)(((byte)(233)))), ((int)(((byte)(233))))), null);
            tbLoginPassword = new cTextBox(new Point(308, 380), new Size(267, 19), new Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))), Color.FromArgb(((int)(((byte)(233)))), ((int)(((byte)(233)))), ((int)(((byte)(233))))), '•');
            lLogErrInfo = new cLabel(new Point(308, 290), new Size(267, 19), new Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))), null, null, "Informacion Login");
            
            this.Controls.Add(btLoginIngregar);
            this.Controls.Add(tbLoginUsuario);
            this.Controls.Add(tbLoginPassword);
            this.Controls.Add(lLogErrInfo);
            
        }

        //Botones
        public cButton btLoginIngregar { get; set; }

        //TextBoxs
        public cTextBox tbLoginUsuario { get; set; }
        public cTextBox tbLoginPassword { get; set; }

        //Label
        public cLabel lLogErrInfo { get; set; }

        

        //FormaPrincipal
        private frmPrincipal frmPrincipal { get; set; }

        public class test
        {
            public frmPrincipal frmPrincipal { get; set; }

            public test(frmPrincipal frmPrincipal)
            {
                this.frmPrincipal = frmPrincipal;
            }

            public void Method()
            {
                frmPrincipal.Invoke((MethodInvoker)delegate()
                {
                    this.frmPrincipal.frmLoad.ShowDialog(this.frmPrincipal);
                });
            }
        }
    }

}
